<?php
class CheckoutController extends AppController{
   
    
    public function index()
    {
        $carts = array();
        if($this->Session->read('carts'))
            $carts = $this->Session->read('carts');
        if(count($carts) ==  0)
            $this->redirect(array('controller' => 'products', 'action' => 'index'));
            
        $this->set('carts', $carts);
        
        $lang_code = $this->Session->read('lang_code');
        $this->loadModel('Category'); $this->Category->lang_code = $lang_code;
        $this->loadModel('Article'); $this->Article->lang_code = $lang_code;
        $category_id = $this->Category->get_category_id_by_code('checkout');
        $artiles = $this->Article->get_article_by_category_id($category_id, 0, 1);
        $contact_checkout = @$artiles[0]['article_desc']['meta_title'];
        $this->set('contact_checkout', $contact_checkout);
        
        
        $breadcrumbs[] = array('title' => 'Danh mục sản phẩm', 'link' => 'danh-muc-san-pham');
        $breadcrumbs[] = array('title' => 'Giỏ hàng', 'link' => 'gio-hang');
        $breadcrumbs[] = array('title' => 'Thanh toán', 'link' => 'checkout');
        $this->set('breadcrumbs', $breadcrumbs);
        
        $meta_title = 'Thanh toán';
        $this->set('meta_title', $meta_title);
        
        $has_html = '';
        $this->set('has_html', $has_html);
        $header_menu_sidebar['title'] = 'Thanh toán';
        $header_menu_sidebar['link'] = 'checkout';
        $this->set('header_menu_sidebar', $header_menu_sidebar);
        $menu_sidebar = array();
        $this->set('menu_sidebar', $menu_sidebar);
    }
    
    public function confirm()
    {
        $lang_code = $this->Session->read('lang_code');
        $carts = array();
        if($this->Session->read('carts'))
            $carts = $this->Session->read('carts');
        if(count($carts) ==  0)
            $this->redirect(array('controller' => 'products', 'action' => 'index'));
            
        $this->set('carts', $carts);
        
        $breadcrumbs[] = array('title' => 'Sản phẩm', 'link' => 'san-pham');
        $breadcrumbs[] = array('title' => 'Giỏ hàng', 'link' => 'gio-hang');
        $breadcrumbs[] = array('title' => 'Thanh toán', 'link' => 'gio-hang');
        $this->set('breadcrumbs', $breadcrumbs);
        $status_checkout = '';
        
        $this->loadModel('Category'); $this->Category->lang_code = $lang_code;
        $this->loadModel('Article'); $this->Article->lang_code = $lang_code;
        $category_id = $this->Category->get_category_id_by_code('checkout');
        $artiles = $this->Article->get_article_by_category_id($category_id, 0, 3);

        $info_checkout = "";
        $successful_checkout = "";
        $content_sendmail = "";
        $content_sendmail_client = "";
        $title_sendmail_client = "";
        if(!empty($artiles))
        {
            if(isset($artiles[0]))
            {
                $info_checkout = $artiles[0]['article_desc']['description'];
            }
            if(isset($artiles[1]))
            {
                $successful_checkout = $artiles[1]['article_desc']['description'];
            }
            if(isset($artiles[2]))
            {
                $content_sendmail = $artiles[2]['article_desc']['long_description'];
                $content_sendmail_client = $artiles[2]['article_desc']['description'];
                $title_sendmail_client = $artiles[2]['article_desc']['title'];
            }
        }
        
        
        
            
            
        if (isset($this->request->data['finish_checkout'])) {
            $this->loadModel('Cart'); $this->Cart->lang_code = $lang_code;
            $user = array();
            $user['user_id'] = $this->request->data['co_user_id'];
            $user['name'] = $this->request->data['co_name'];
            $user['email'] = $this->request->data['co_email'];
            $user['phone'] = $this->request->data['co_phone'];
            $user['address'] = $this->request->data['co_address'];
            
            $result = $this->Cart->add_cart($user, $carts);
            $status_checkout = $result['status_checkout'];
            $this->set('result', $result);
            if(!empty($result))
            {
                $this->loadModel('Setting'); $this->Setting->lang_code = $lang_code;
                $settings = $this->Setting->get_setting();
                
                $cart_id = $result['cart_id'];
                $successful_checkout = str_replace('[cart_id]', $cart_id , $successful_checkout);
                $this->set('cart_id', $cart_id);
                
                $this->Session->delete('carts');
                
                $order_link = Router::url('/', true) . 'checkout/review/' . base64_encode($cart_id);
                $order_search = Router::url('/', true) . 'checkout/search';
                $search = array('[order_link]', '[client_name]', '[phone]', '[address]', '[order_id]', '[order_search]');
                $replace = array($order_link, $user['name'], $user['phone'], $user['address'], $cart_id, $order_search);
                
                $Email = new CakeEmail();
                $Email->config('smtp');
                $Email->emailFormat('html');


                try {              
                    $Email->to($user['email']);
                    $Email->subject($title_sendmail_client);
                    $Email->send(str_replace($search, $replace, $content_sendmail_client));
                    
                    $Email->to(@$settings['email_send_request']);
                    $Email->subject('Có đơn đặt hàng từ Website');
                    $Email->send(str_replace($search, $replace, $content_sendmail));
                } catch (Exception $e) {
                    $result = array('status_checkout' => $e->getMessage());
                } 
            }
        }
        
        $this->set('info_checkout', $info_checkout);
        $this->set('successful_checkout', $successful_checkout);
        $this->set('status_checkout', $status_checkout);
        
        $meta_title = 'Xác nhận đơn hàng';
        $this->set('meta_title', $meta_title);
        
        $has_html = '';
        $this->set('has_html', $has_html);
        $header_menu_sidebar['title'] = 'Thanh toán';
        $header_menu_sidebar['link'] = 'checkout';
        $this->set('header_menu_sidebar', $header_menu_sidebar);
        $menu_sidebar = array();
        $this->set('menu_sidebar', $menu_sidebar);
    }
    
    public function review($cart_id = null)
    {
         if($cart_id == null)
         {
            $this->redirect(array('controller' => 'products', 'action' => 'index'));
         }
         else
         {
            $lang_code = $this->Session->read('lang_code');
            $cart_id = base64_decode($cart_id);
            $this->loadModel('Cart'); $this->Cart->lang_code = $lang_code;
            $cart_info = $this->Cart->get_info_cart($cart_id);
            $this->set('cart_info', $cart_info);
            $this->set('cart_id', $cart_id);
         }
         
        $breadcrumbs[] = array('title' => 'Thông tin đơn hàng', 'link' => 'checkout/review/'.base64_encode($cart_id));
        $this->set('breadcrumbs', $breadcrumbs);
        
        $meta_title = 'Xem lại đơn hàng';
        $this->set('meta_title', $meta_title);
        
        $has_html = '';
        $this->set('has_html', $has_html);
        $header_menu_sidebar['title'] = 'Thông tin đơn hàng';
        $header_menu_sidebar['link'] = 'checkout/review/'.base64_encode($cart_id);
        $this->set('header_menu_sidebar', $header_menu_sidebar);
        $menu_sidebar = array();
        $this->set('menu_sidebar', $menu_sidebar);
        
    }
    
    public function search()
    {
        $breadcrumbs[] = array('title' => 'Tìm kiếm đơn hàng', 'link' => 'checkout/search');
        $this->set('breadcrumbs', $breadcrumbs);
        $lang_code = $this->Session->read('lang_code');
        if($this->request->isPost())
        {
            $info_cart = array();
            $search_cart = $this->request->data['search_cart'];
            
            $prefix_cart_id = Configure::read('prefix_cart_id');
            $this->loadModel('Cart'); $this->Cart->lang_code = $lang_code;
            if(strtoupper(substr($search_cart,0,2)) == $prefix_cart_id)
            {
                $search_cart = strtoupper($search_cart);
                $info_cart = $this->Cart->get_info_cart($search_cart);
            }
            else
            {
                $info_cart = $this->Cart->get_info_cart_by_email($search_cart);
            }
            $this->set('info_cart', $info_cart);
        }
        
        $meta_title = 'Tìm kiếm đơn hàng';
        $this->set('meta_title', $meta_title);
        
        $has_html = '';
        $this->set('has_html', $has_html);
        $header_menu_sidebar['title'] = 'Tìm kiếm đơn hàng';
        $header_menu_sidebar['link'] = 'checkout/search';
        $this->set('header_menu_sidebar', $header_menu_sidebar);
        $menu_sidebar = array();
        $this->set('menu_sidebar', $menu_sidebar);
    }
}